package com.source.hotelbath.utils;

import android.content.Context;
import android.os.Build;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cookie.store.SPCookieStore;
import com.source.hotelbath.net.CustomerRequestConstants;

import java.net.CookieStore;
import java.net.URL;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.HttpUrl;


/**
 * Created by ZhouMeng on 2017/9/29.
 */

public class MyCookieStore {

    private static CookieSyncManager cookieSyncManager;
    public static CookieStore cookieStore = null;
    public static String JSESSIONID = null;
    public static String result = null;
    //在mWebView.loadUrl(url); 之前给url添加上cookies
    public static void setCookiesToURL(String url, Context context) {
        try {
            List<Cookie> cookies1 = new SPCookieStore(context).getCookie(HttpUrl.get(new URL(CustomerRequestConstants.newUrl+CustomerRequestConstants.LOGIN_COMMOND)));
            if (cookies1 != null && !cookies1.isEmpty()) {
                if (null == cookieSyncManager) {
                    cookieSyncManager = CookieSyncManager.createInstance(context);
                }
                CookieManager cookieManager1 = CookieManager.getInstance();
                cookieManager1.setAcceptCookie(true);
                cookieManager1.removeSessionCookie();// 移除
                for (Cookie cookie1 : cookies1) {
                    Cookie sessionInfo = cookie1;
                    String cookieString = sessionInfo.value() + "=" + JSESSIONID + ";";
                    cookieSyncManager.startSync();
                    new Thread().sleep(500);// 刷新 webview.db,不然不会得到实时效果
                    cookieManager1.setCookie(url, cookieString);
                }

                String cook = cookieManager1.getCookie(url);
                System.out.println("getCookie:  -----------" + cook);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static void synCookies(Context context, String url,String value) {
        //一般手动取出cookie的目的只是交给 webview 等等，非必要情况不要自己操作


//        showToast(httpUrl.host() + "对应的cookie如下：" + cookies.toString());
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
//        cookieManager.removeSessionCookie();//移除

        cookieManager.setCookie(url, value);//cookies是在HttpClient中获得的cookie
        if (Build.VERSION.SDK_INT < 21) {
            CookieSyncManager.getInstance().sync();
        } else {
            CookieManager.getInstance().flush();
        }
//        CookieSyncManager.getInstance().sync();
    }
    public static void removeCookie(Context context){
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();//移除
    }
}
